package com.dkm.ec.sale.repository.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.codec.net.QCodec;
import org.springframework.stereotype.Repository;

import com.dkm.base.api.model.Page;
import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.db.persistence.dao.IQueryDao;
import com.dkm.biz.repository.AbstractRepository;
import com.dkm.ec.product.persistence.dao.ProductMediaQueryDao;
import com.dkm.ec.product.persistence.entity.ProductMediaPo;
import com.dkm.ec.sale.domain.SaleEntity;
import com.dkm.ec.sale.persistence.dao.SaleEntityQueryDao;
import com.dkm.ec.sale.persistence.dao.SalePackageItemQueryDao;
import com.dkm.ec.sale.persistence.entity.SaleEntityPo;
import com.dkm.ec.sale.persistence.entity.SalePackageItemPo;
import com.dkm.ec.sale.persistence.entity.SaleProdPo;
import com.dkm.ec.sale.repository.SaleEntityRepository;

/**
 * 对象功能:销售产品实体 Repository接口的实现类 开发公司:SKG.COM 开发人员:Yuki 创建时间:2015-01-16 15:23:27
 */
@Repository
public class SaleEntityRepositoryImpl extends AbstractRepository<String, SaleEntityPo, SaleEntity> implements SaleEntityRepository {

	@Resource
	private SaleEntityQueryDao saleEntityQueryDao;

	@Resource
	private SalePackageItemQueryDao salePackageItemQueryDao;

	@Resource
	private ProductMediaQueryDao productMediaQueryDao;

	public SaleEntity newInstance() {
		SaleEntityPo po = new SaleEntityPo();
		SaleEntity saleEntity = SpringHelper.getBean(SaleEntity.class);
		saleEntity.setData(po);
		return saleEntity;
	}

	public SaleEntity newInstance(SaleEntityPo po) {
		SaleEntity saleEntity = SpringHelper.getBean(SaleEntity.class);
		saleEntity.setData(po);
		return saleEntity;
	}

	@Override
	protected IQueryDao<String, SaleEntityPo> getQueryDao() {
		return saleEntityQueryDao;
	}
	
	@Override
	public List<SaleEntityPo> find2(Map<String,Object> paraMap) {
		return saleEntityQueryDao.find2(paraMap);
	}		

	@Override
	public SaleEntityPo getByKey(String key) {
		return saleEntityQueryDao.getSaleEntityPoByKey(key);
	}

	@Override
	public List<SaleEntityPo> findProdRec(String userId, String productId, Integer count) {
		// TODO Auto-generated method stub
		return saleEntityQueryDao.findProdRec(userId, productId, count);
	}

	@Override
	public List<SaleProdPo> findProdByUpdateTime(String startTime, String endTime,Page page) {
		List<SaleProdPo> sppList = saleEntityQueryDao.findProdByUpdateTime(startTime, endTime,page);
		List<SaleProdPo> newsppList = new ArrayList<SaleProdPo>();
		if (null != sppList && sppList.size() > 0) {
			for (SaleProdPo spp : sppList) {
				SaleProdPo newsppPo = new SaleProdPo();
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("whereSql", "sku_id_ = '" + spp.getId() + "'");
				newsppPo = spp;
				if ("package".equals(spp.getProductType())) {
					List<SalePackageItemPo> list = salePackageItemQueryDao.findAll(params);
					String itemSkuId = "";
					if (null != list && list.size() > 0) {
						for (SalePackageItemPo s : list) {
							if (!s.getItemSkuId().equals(spp.getId())) {
								itemSkuId += s.getItemSkuId() + ",";
							}
						}
						if (itemSkuId.length() > 0) {
						 newsppPo.setExtJson("{\"packages\":\""+itemSkuId.substring(0, itemSkuId.length() - 1)+"\"}");
						}
					}
					newsppPo.setProductType("P");
				} else if ("entity".equals(spp.getProductType())) {
					newsppPo.setProductType("C");
				} else if ("service".equals(spp.getProductType())) {
					newsppPo.setProductType("S");
				} else {
					newsppPo.setProductType("V");
				}
				// 获取attrOpIds下标
				String attrIds = spp.getAttrIds();
				if (null != attrIds && attrIds.contains(",")) {
					String attrOpId = "";
					String[] attridarr = attrIds.split(",");
					String[] attrOpIdarr = spp.getAttrOpIds().split(",");
					String attrId = saleEntityQueryDao.findAttrIdByEavSetKey(spp.getEavSetKey());
					for (int i = 0; i < attridarr.length; i++) {
						if (attrId.equals(attridarr[i])) {
							attrOpId = attrOpIdarr[i];
						}
					}
					ProductMediaPo pmp = productMediaQueryDao.getMainMediaPoByProdIdAttrOpId(spp.getSpid(), attrOpId);
					if (null != pmp) {
						newsppPo.setSmallImage(pmp.getCloudPath());
					}
				}

				newsppList.add(newsppPo);
			}
		}
		return newsppList;
	}

	@Override
	public int findProdByUpdateTimeCount(String startTime, String endTime) {
		return saleEntityQueryDao.findProdByUpdateTimeCount(startTime, endTime);
	}

/*	@Override
	public List<SaleEntityPo> findByRequst(String cateId, String requestType, Page page) {
		Map<String, Object> params = new HashMap<>();
		params.put("cateId", cateId);
		params.put("requestType", requestType);
		return saleEntityQueryDao.findByKey("findByRequst", params, page);
	}*/

	@Override
	public List<SaleEntityPo> findProductRec(String indexProduct) {
		Map<String, Object> params = new HashMap<>();
		params.put("srcType", indexProduct);
		return saleEntityQueryDao.findByKey("findProductRec", params);
	}

	@Override
	public List<SaleEntityPo> findShopProduct(String shopId) {
		Map<String, Object> params = new HashMap<>();
		params.put("shopId", shopId);
		return saleEntityQueryDao.findByKey("findShopProduct", params);
	}

	@Override
	public List<SaleEntityPo> findShopProductByOther(String saleName,String shopId, Page page, String type, String order, String cateId,String shopName) {
		Map<String, Object> params = new HashMap<>();
		params.put("name", saleName);
		params.put("shopId", shopId);
		params.put("type", type);
		params.put("order", order);
		params.put("cateId", cateId);
		params.put("shopName", shopName);
		return saleEntityQueryDao.findByKey("findShopProductByOther", params,page);
	}
}
